/**
 *  Copyright (c)  2010  Angel Ortiz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;

/**
 * This class is a GUI for interactive tables of units
 * 
 * @author Angel Ortiz
 * @date 02/12/2010
 */
public class UnitOverview extends JPanel implements ActionListener {

	protected Font headerTile = new Font("timesroman", Font.BOLD, 30);
	protected Font headerSubtitle = new Font("serif", Font.ITALIC, 18);
	protected Font regularText = new Font("monospaced",
			Font.LAYOUT_LEFT_TO_RIGHT, 14);
	protected final String[] COL_NAMES = { " Id ", " Type ", " Offensive ",
			" Deffensive ", " Armor ", " Movement ", " Health ", " Upkeep ",
			" Player's Id ", " Army's Id ", " Mission " };
	protected KeyboardListener kl = null;
	protected Player player = null;
	protected JTable table = null;
	protected TableModelObj tableModel = null;
	protected JScrollPane scrollPane = null;
	protected View view = null;

	public UnitOverview(Player player, KeyboardListener kl, View view) {
		super(new BorderLayout());
		this.player = player;
		this.kl = kl;
		this.view = view;
		this.addKeyListener(kl);
		loadGUI();
	}

	/**
	 * Draws all GUI components to be contained in this paneled form
	 */
	public void loadGUI() {
		tableModel = new TableModelObj(this.COL_NAMES, this
				.getTableDataFromPlayer());
		table = new JTable(tableModel);
		table.setPreferredScrollableViewportSize(new Dimension(980, 96));
		table.setCellSelectionEnabled(false);
		scrollPane = new JScrollPane();
		scrollPane.setViewportView(table);
		JPanel titlesPanel = new JPanel(new GridLayout(2, 0));
		Border border = BorderFactory.createMatteBorder(0, 0, 5, 0, new Color(
				15, 20, 95));

		JLabel titleLabel = new JLabel("Unit Overview");
		titleLabel.setFont(headerTile);
		titleLabel.setBorder(border);

		JLabel subTitleLabel = new JLabel(
				"A summary of all game unit statistics");
		subTitleLabel.setFont(headerSubtitle);

		JPanel buttonsPanel = new JPanel(new GridLayout(2, 0));

		JButton goToMainBtn = new JButton("Go to Main Screen");
		goToMainBtn.setMnemonic(KeyEvent.VK_G);
		goToMainBtn.addActionListener(this);

		JButton goToStructsBtn = new JButton("Go to Structures Overview");
		goToStructsBtn.setMnemonic(KeyEvent.VK_S);
		goToStructsBtn.addActionListener(this);

		buttonsPanel.add(goToMainBtn);
		buttonsPanel.add(goToStructsBtn);

		titlesPanel.add(titleLabel);
		titlesPanel.add(subTitleLabel);

		this.add(titlesPanel, BorderLayout.PAGE_START);
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(buttonsPanel, BorderLayout.PAGE_END);

	}

	/**
	 * Returns a summary of all statistics from units in the game in and object
	 * array 2D
	 * 
	 * @return - structures statistics
	 */
	public Object[][] getTableDataFromPlayer() {

		int count = 0;
		int[][] tempSize = player.getArrayOfSizes();
		Tileable[][][] tileables = player.getTileables();
		int size = 0;
		for (int i = 0; i < tileables[0].length; i++) {
			size += tempSize[0][i];
		}

		Object[][] tempObj = new Object[size][this.COL_NAMES.length];

		for (int i = 0; i < tileables[0].length; i++) {
			for (int j = 0; j < tempSize[0][i]; j++) {
				for (int k = 0; k < this.COL_NAMES.length - 1; k++) {
					if (k == 0) {
						tempObj[count][k] = (tileables[0][i][j].getStatus()[0]);
					} else if (k == 1) {
						tempObj[count][k] = tileables[0][i][j]
								.getTypeName(tileables[0][i][j].getStatus()[k]);
					} else {
						tempObj[count][k] = tileables[0][i][j].getStatus()[k];
					}
				}
				tempObj[count][this.COL_NAMES.length - 1] = ((Unit) tileables[0][i][j])
						.getCommandTxt();
				count++;
			}
		}
		return tempObj;
	}
	/**
	 * Controls through the action event of the buttons in the panel,
	 * which panel gets focus; Overrides parent
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equalsIgnoreCase("go to main screen")) {
			this.setEnabled(false);
			this.setFocusable(false);
			this.setVisible(false);

			this.enableInputMethods(false);
			view.setFocus(0);
		} else if (e.getActionCommand().equalsIgnoreCase(
				"Go to Structures Overview")) {
			this.setEnabled(false);
			this.setFocusable(false);
			this.setVisible(false);

			this.enableInputMethods(false);
			view.setFocus(2);
		}

	}
	/**
	 * Updates data model of interactive tables with fresh data
	 */
	public void refresh() {
		this.tableModel.setData(this.getTableDataFromPlayer());
		this.table.setModel(this.tableModel);
		this.scrollPane.setViewportView(this.table);
	}
}
